(function(app){
	app
		.controller("content_05_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$state","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$state,$q,$ionicHistory){
			
			//接收参数
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var searchParam = $stateParams.searchParam;
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var initFlag = false;
			
			//绑定参数到页面
			$scope.chdrnum01 = $stateParams.chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			var token=sessionStorage.getItem("token");
			
/***************************************************************初始化部分begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.item05 = itemData.item05;
						
						//《申请须知默认勾选》
						//$scope.readFlag = true;//阅读协议标志，默认为false
						
						//初始化下拉框，取与现有方式相反的方式，如果为空，则默认第一个
						var oldGetMode = $scope.item05.bonusGetModeNameOld;//原来的获取方式
						if("储存生息"==oldGetMode){//如果初始方式是储存生息，那么换到抵缴保费，否则默认第一种
							$scope.bonusGetModeSelect = "PMST";
							$("#bonusGetModeSelect").val("PMST");
							$("#bonusGetModeInput").val("抵缴保费");
						}else{
							$scope.bonusGetModeSelect = "DVAC";
							$("#bonusGetModeSelect").val("DVAC");
						}
						
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();//失败时候直接隐藏加载圈，用户才可以点返回然后重新进入
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/***************************************************************初始化部分end****************************************************/
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}

			//改变已阅读标志
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
				console.log($scope.readFlag);
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn05Input','overwindowOn05Input','policyCloseXOn05Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;//绑定至页面
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn05Input = function(){
				modal('close','policyModalOn05Input','overwindowOn05Input','policyCloseXOn05Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
//			//选择新的红利领取方式
//			$scope.reasonChose = function(){
//				modal('open','reason_model','overwindowOn05Input');
//			}
//			$scope.reasonClose = function(){
//				modal('close','reason_model','overwindowOn05Input');
//				//alert($("#newWelfare").val());
//				//$.toast("信函接收方式没有改变不需要改变", "text");
//			}
//			$scope.reasonClick = function (val) {
//				$scope.data.reasonVal = val;
//			}
			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn05Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn05Input');
			}
			//选择新的红利领取方式
			$scope.changeBonusGetMode = function(){
				var options =$("#bonusGetModeSelect option:selected");
				$("#bonusGetModeInput").val(options.text());
				console.log(options.text());
				console.log(options.val());
			}
			
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}	
			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入项目
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				
				if($scope.readFlag == false){
					myFactory.alert("","请先同意《申请须知》!","info");
					return;
				}
				
				/*var a = $("#bonusGetModeNameOld").text();
				if(a=="储存生息"){
					a="储存生息/留存公司";
				}*/
				var a =$("#bonusGetModeCodeOld").text();
				
				//var b=$.trim($("select[name=BonusGetMode]").find("option:selected").val());
				var options =$("#bonusGetModeSelect option:selected");
		    	//var b = options.text();
		    	var b = options.val();
		    	
		        if(a==b){
		        	myFactory.alert("","红利领取方式没有改变不需变更!","info");
					return;
		        }
		        
		        var item = {
		        	"bonusGetMode" : options.val(),
		        	"itemId":itemId
		        }
		        
		        $ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
		        
			}
					
		}]);    
	
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)